package com.ksd.modules.download.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.download.po.AdminDownloadsCodeSavePO;
import com.ksd.modules.download.po.AdminDownloadsCodeSearchPO;
import com.ksd.modules.download.service.IAdminDownloadsCodeService;
import com.ksd.modules.download.vo.AdminDownloadsCodeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class AdminDownloadsCodeController extends BaseController {

    @Autowired
    private IAdminDownloadsCodeService adminDownloadsCodeService;

    /**
     * 【下载资源】-【兑换码】- 列表
     * @return
     */
    @PostMapping("/download/code/list")
    public R<Page<AdminDownloadsCodeVo>> findAdminDownloadss(@Validated @RequestBody AdminDownloadsCodeSearchPO adminDownloadsCodeSearchPO){
        return R.data(adminDownloadsCodeService.findAdminDownloadsCodes(adminDownloadsCodeSearchPO));
    }


    /**
     * 【下载资源】 - 创建兑换码
     * @param adminDownloadsCodeSavePO
     * @return
     */
    @PostMapping("/download/code/create")
    @ApiLog("【下载资源】 - 创建兑换码")
    @RepeatSubmit
    public R<Boolean> createBatchCode(@Validated @RequestBody AdminDownloadsCodeSavePO adminDownloadsCodeSavePO){
        return R.data(adminDownloadsCodeService.createBatchCode(adminDownloadsCodeSavePO));
    }

    /**
     * 【下载资源】-【兑换码】-批量彻底删除
     *
     * @return
     */
    @PostMapping("/download/code/batch/remove")
    @ApiLog("【下载资源】-【兑换码】-批量彻底删除")
    @RepeatSubmit
    public R<Boolean> batchRemovesAdminDownloads(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminDownloadsCodeService.batchRemovesAdminDownloadsCode(batchIdsPO));
    }

    /**
     * 【下载资源】-【兑换码】-根据ID删除
     * @param id
     * @return
     */
    @PostMapping("/download/code/remove/{id}")
    @ApiLog("【下载资源】-【兑换码】-根据ID删除")
    public R<Boolean> removeAdminDownloadsById(@PathVariable("id")Long id){
        return R.data(adminDownloadsCodeService.removeAdminDownloadsCodeById(id));
    }

    /**
     *【下载资源】-【兑换码】-修改状态
     * @return
     */
    @PostMapping("/download/code/status/{id}/{field}")
    @ApiLog("【下载资源】-【兑换码】-修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @RequestParam("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminDownloadsCodeService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminDownloadsCodeService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }
}
